// index.js
import { PNT, setNavBarTitle, pushPagePath } from '../../../../utils/utils';
import { GetAddressList, setDefaultAddress } from '../../../../utils/apis';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressLst: [
      { name: '张三', tel: '18235160000', address: '北京市朝阳区劲松东三环富顿中心', is_default:'1'},
      { name: '李四', tel: '11111', address: 'ddddddddddddd' },
      { name: '旺旺', tel: '2222222222', address: 'ddddddddddddd' }
    ]
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    //setNavBarTitle(PNT.assets.main);
    this.loginModal = this.selectComponent("#login-modal");
    var info = wx.getStorageSync('userInfo');
    this.setData({
      info: info,
    })
    this.getAddress();
  },

  /**
   * 收货地址
   * @author darlang
   */
  getAddress() {
    if(!this.loginModal.check()){
      return false;
    }
    var info = wx.getStorageSync('userInfo');
    GetAddressList(info.user_id).then(res=>{
      if(res.error_code == 10000){
        this.setData({
          addressLst:res.data,
          userInfo:info,
        })
      }
    })
   
  },

  /**
   * 页面跳转
   * @author darlang
   */
  pushPath(e) {
    const pathData = [
      { type: 'add', path: '/pages/member/address/edit/edit?id='+e.currentTarget.dataset.id },
    ];
    pushPagePath(e, pathData);
  },


  /**
   * 设定默认收货地址
   * @author darlang
   */
  setDefaultAddress(e) {
    var self = this;
    var data = {};
    data.user_id = this.data.info.user_id;
    data.address_id = e.currentTarget.dataset.id;
    data.type = e.currentTarget.dataset.type;
    if(data.type == 'del'){
      wx.showModal({
        title: '删除地址',
        content: '确认删除地址?',
        success(res) {
          if (res.confirm) {
            setDefaultAddress(data).then(res => {
              if (res.error_code == 10000) {
                self.getAddress();
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      setDefaultAddress(data).then(res => {
        if (res.error_code == 10000) {
          self.getAddress();
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      addressLst: ''
    });
    this.getAddressList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getAddressList();
  }
});